package com.system.controller;

import com.common.annotation.MyLog;
import com.common.constants.Constant;
import com.common.controller.BaseController;
import com.system.entity.User;
import com.system.service.UserService;
import com.system.vo.request.*;
import com.system.vo.response.PageVO;
import com.system.vo.response.UserOwnRoleRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import com.common.res.DataResult;

/**
* @author XueHeng
* @since 2020-09-17
*/
@RestController
@RequestMapping("api")
@Api(tags = "用户管理")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    @ApiOperation("获取当前用户信息")
    @MyLog(title = "用户管理",action = "获取当前用户信息")
    @RequiresPermissions("sys:user:list")
    public DataResult<User> getUser(HttpServletRequest request){
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        result.setData(this.userService.getUser(userId));
        return result;
    }

    @PutMapping("user/info")
    @ApiOperation("修改当前用户信息")
    @MyLog(title = "用户管理",action = "修改当前用户信息")
    @RequiresPermissions("sys:user:update")
    public DataResult updateUser(@RequestBody User user,HttpServletRequest request){
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        this.userService.updateUser(user,userId);
        return result;
    }

    @PutMapping("user/pwd")
    @ApiOperation("修改当前用户密码")
    @MyLog(title = "用户管理",action = "修改当前用户密码")
    @RequiresPermissions("sys:user:update")
    public DataResult updateUserPWD(@RequestBody UserUpdatePwdReqVO vo, HttpServletRequest request){
        DataResult result =DataResult.success();
        String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
        String refresgToken=request.getHeader(Constant.REFRESH_TOKEN);
        this.userService.updatePwd(vo,accessToken,refresgToken);
        return result;
    }

    @GetMapping("/user/logout")
    @ApiOperation("退出系统")
    @MyLog(title = "用户管理",action = "退出系统")
    public DataResult logout(HttpServletRequest request){
        DataResult result =DataResult.success();
        String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
        String refresgToken=request.getHeader(Constant.REFRESH_TOKEN);
        this.userService.logout(accessToken,refresgToken);
        return result;
    }

    @PostMapping("/users")
    @ApiOperation("用户列表")
    @MyLog(title = "用户管理",action = "获取用户列表")
    @RequiresPermissions("sys:user:list")
    public DataResult<PageVO<User>> getAllUsers(@RequestBody UserPageReqVO vo){
        DataResult result =DataResult.success();
        result.setData(this.userService.pageInfo(vo));
        return result;
    }

    @PostMapping("/user")
    @ApiOperation("新增")
    @MyLog(title = "用户管理",action = "新增用户信息")
    @RequiresPermissions("sys:user:add")
    public DataResult add(@Valid @RequestBody UserAddReqVO vo,HttpServletRequest request) {
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        this.userService.add(vo,userId);
        return result;
    }

    @PutMapping("/user")
    @ApiOperation("更新")
    @MyLog(title = "用户管理",action = "更新用户信息")
    @RequiresPermissions("sys:user:update")
    public DataResult update(@Valid @RequestBody UserUpdateReqVO vo,HttpServletRequest request) {
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        this.userService.update(vo,userId);
        return result;
    }


    @DeleteMapping(value = "/user")
    @ApiOperation("删除")
    @MyLog(title = "用户管理",action = "删除用户信息")
    @RequiresPermissions("sys:user:delete")
    public DataResult remove(@RequestBody  List<String> userIds, HttpServletRequest request) {
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        this.userService.delete(userIds,userId);
        return result;
    }


    @GetMapping("/user/token")
    @ApiOperation("获取业务token")
    @MyLog(title = "用户管理",action = "获取业务token")
    public DataResult<String> token(HttpServletRequest request){
        DataResult result =DataResult.success();
        String refreshToken=request.getHeader(Constant.REFRESH_TOKEN);
        result.setData(this.userService.token(refreshToken));
        return result;
    }

    @GetMapping("/user/roles/{userId}")
    @RequiresPermissions("sys:user:role:update")
    @ApiOperation("查询用户拥有的角色")
    @MyLog(title = "用户管理",action = "查询用户拥有的角色")
    public DataResult<UserOwnRoleRespVO> getRoles(@PathVariable("userId") String userId){
        DataResult result = DataResult.success();
        result.setData(this.userService.getUserOwnRole(userId));
        return result;
    }

    @PutMapping("/user/roles")
    @ApiOperation("赋予用户角色")
    @MyLog(title = "用户管理",action = "赋予用户角色")
    @RequiresPermissions("sys:user:role:update")
    public DataResult roles(@RequestBody @Valid UserOwnRoleReqVO vo){
        DataResult result = DataResult.success();
        this.userService.updateUserRole(vo);
        return result;
    }
}
